<?php
namespace app\admin\controller\v1;

use app\admin\validate\CreateAdmin;
use app\admin\validate\CreateAdminRole;
use app\common\model\AdminRole as AdminRoleModel;
use app\common\model\Admin as AdminModel;


class Admin extends BaseController
{
    public function createOne(){
        $validate = new CreateAdmin();
        //验证
        $validate->goCheck();
        $data = $validate->request->post();
        //密码加盐md5处理
        $salt = getRandChar(10);
        $data['salt'] = $salt;
        $data['password'] = md5($data['password'].$salt);
        AdminModel::createOne($data);
        return successReturn();

    }
    public function getAll()
    {
        $admins = AdminModel::getAll();
        return successReturn($admins);
    }
    public function getById($id)
    {
        $admin = AdminModel::getById($id);
        return successReturn($admin);
    }
    public function deleteOne($id){
        AdminModel::deleteOne($id);
        return successReturn();
    }

    public function updateOne($id){
        $data = input('put.');
        AdminModel::updateOne($id,$data);
        return successReturn();
    }
    public function updateSelf(){
        $data = input('put.');
//        User::get
//        AdminModel::updateSelf($user->id,$data);
//        return successReturn();
    }
    //为管理员关联角色
    public function matchRole(){
        //验证数据合法性
        (new CreateAdminRole())->goCheck();
        AdminRoleModel::matchRole(input('post.'));
        return successReturn();
    }
    //解绑管理员角色
    public function unMatchRole(){
        AdminRoleModel::unMatchRole(input('delete.'));
        return successReturn();
    }
}
